﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WP.Commons;
using WP.DB;
using WP.Modules;


using WP.RM.BusinessRule;
using WP.RM.BusinessLogical;
using WP.RM.DataAccess;

namespace WP.RM.Form
{
    public partial class frmRMProductMtn : BaseMtn 
    {
        public frmRMProductMtn()
        {
            InitializeComponent();
            LoadAllControl(new ArrayList(this.Controls));
            this.MtnTabControl = this.tbcMain;
            this.DesignTabsName = "tbpGeneral";
            tbcMain.DrawMode = TabDrawMode.OwnerDrawFixed;
            this.MinimumSize = this.Size;
        }
                
        private void FillTree()
        {
            DBAccess dbcon = new DBAccess(false, false);
            string strSQL = "select TypeId,TypeName,RecID from RMProductFirstType order by TypeId";
            DataTable dtItem = dbcon.GetDataTable(strSQL);
            foreach (DataRow drType in dtItem.Rows)
            {
                TreeNode tvNode = new TreeNode(CvtFunc.ToString(drType["TypeId"]) + "-" + CvtFunc.ToString(drType["TypeName"]));
                this.tvTree.Nodes.Add(tvNode);
                int intRow = 0;
                strSQL = "select TypeId,TypeName from RMProductSecondType where parentid='" + CvtFunc.ToString(drType["RecID"]) + "'";
                DataTable dtDtl = dbcon.GetDataTable(strSQL);
                foreach (DataRow drDtl in dtDtl.Rows)
                {
                    tvNode.Nodes.Add(CvtFunc.ToString(drDtl["TypeId"]) + "-" + CvtFunc.ToString(drDtl["TypeName"]));
                }
            }
            dbcon.CloseCon();
        }



        #region Define Variable
        private bool mblnSettingStatus = false;
        #endregion

        #region ***Form Events

        private void frmRMProductMtn_Load(object sender, EventArgs e)
        {
            this.TableName = "rmProduct";
            //dgList.ColumnWidth = "0,70,180,100,40,20,120,90,130";
            dgList.ConnectionString = DBAccess.ConnectionDBString;
            dgList.TableName = this.TableName;
            dgList.ColumnWidth = DataFunctions.GetGridColumnWidthString(this.Name, this.CurrentMenuInfo.iParameter, SysVar.UserName, this.Name + "." + this.dgList.Name);
            string strSql = WP.Commons.BaseFunctions.GetLookUpSQL(this.TableName, this.CurrentMenuInfo.iParameter);
            dgList.Sql = strSql;
            this.formStatus = FormStatus.Query;

            FillLookUpInfo();
            FillTree();
        }
        private void frmRMProductMtn_FormClosing(object sender, FormClosingEventArgs e)
        {
            DataFunctions.SetGridColumnWidthString(this.Name, this.CurrentMenuInfo.iParameter, SysVar.UserName, this.dgList);
        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!this.UpdateData()) return;
            base.SetStatusAfterUpdate();
        }
        private void btnNew_Click(object sender, EventArgs e)
        {
            if (this.ConfirmChange())
            {
                if (!this.UpdateData()) return;
            }
            this.formStatus = FormStatus.AddNew;
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (!this.DeleteData()) return;
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.formStatus = FormStatus.Delete;
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            if (this.formStatus == FormStatus.AddNew || this.formStatus == FormStatus.Update)
            {
                if (this.ConfirmChange())
                {
                    if (MessageBox.Show(SysVar.SaveChangeTitle, SysVar.WarnTitle, MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        if (!this.UpdateData()) return;
                    }
                }
            }
            this.ExitingStatus = true;
            this.Close();
        }
        private void btnHelp_Click(object sender, EventArgs e)
        {
            base.OnHelp();
        }
        private void btnToExcel_Click(object sender, EventArgs e)
        {
            base.ExportToExcel(dgList);
        }
        private void btnNext_Click(object sender, EventArgs e)
        {
            Nevigate(this.dgList, NavigateOrient.Next);
        }
        private void btnPrev_Click(object sender, EventArgs e)
        {
            Nevigate(this.dgList, NavigateOrient.Prev);
        }
        private void btnTop_Click(object sender, EventArgs e)
        {
            Nevigate(this.dgList, NavigateOrient.Top);
        }
        private void btnBottom_Click(object sender, EventArgs e)
        {
            Nevigate(this.dgList, NavigateOrient.Bottom);
        }
        public override void SetNavigatorBarTooltip()
        {
            if (this.RecID <= 0)
            {
                this.btnBottom.ToolTipText = "定位到末页";
                this.btnTop.ToolTipText = "定位到首页";
                this.btnNext.ToolTipText = "定位到下一页";
                this.btnPrev.ToolTipText = "定位到前一页";
            }
            else
            {
                this.btnBottom.ToolTipText = "定位到末记录";
                this.btnTop.ToolTipText = "定位到首记录";
                this.btnNext.ToolTipText = "定位到下一条记录";
                this.btnPrev.ToolTipText = "定位到上一条记录";
            }
        }

        private void dgList_DefineColumn(object sender, EventArgs e)
        {
            base.SelectListViewField(dgList);
        }
        private void dgList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && dgList.CurrentRow != null)
            {
                this.RecID = Convert.ToInt64(dgList.RecID);
            }
        }
        private void dgList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (dgList.CurrentRow != null && BaseFunctions.IsDataGridCell(this.dgList, e))
            {
                this.RecID = Convert.ToInt64(dgList.RecID);
            }
        }

        protected override void Field_Leave(object sender, System.EventArgs e)
        {
            base.Field_Leave(sender, e);
            Control con = sender as Control;
            if (con == null) return;

            //below is some speci field , need do some speci work
            
        }
        protected override void Field_Change(object sender, EventArgs e)
        {
            base.Field_Change(sender, e);
            if (this.formStatus == FormStatus.Delete) return;
            Control con = sender as Control;
            if (con == null) return;
            DBAccess dbcon = new DBAccess(false, false);
            if (con.Name == txtSetName.Name && this.formStatus == FormStatus.AddNew)
            {
                string strSQL = string.Empty;
                strSQL = "select * from rmSet where SetName ='" + con.Text + "'";
                DataRow drSet = dbcon.GetDataFirstRow(strSQL);
                if (drSet != null)
                {
                    txtBaseSetSize.Text = CvtFunc.ToString(drSet["SetSize"]);
                    txtBaseProductLength.Text = CvtFunc.ToString(drSet["Length"]);
                    txtBaseProductWidth.Text = CvtFunc.ToString(drSet["Width"]);
                }

            }

            if (con.Name == txtSetID.Name && this.formStatus == FormStatus.AddNew)
            {
                txtSetName.Text =DataFunctions.GetFieldValue( "rmSet","SetName","SetID='"+ txtSetID.Text +"'");
            }
            else if (con.Name == txtCategoryID.Name && this.formStatus == FormStatus.AddNew)
            {
                txtCategoryName.Text = DataFunctions.GetFieldValue("rmCategory", "CategoryName", "CategoryID='" + txtCategoryID.Text + "'");
            }

            if ((con.Name == txtCategoryName.Name || con.Name == txtSetName.Name) && this.formStatus == FormStatus.AddNew)
            {
                txtCategoryName.Text = DataFunctions.GetFieldValue ("rmCategory","CategoryName","CategoryID='"+ txtCategoryID.Text +"'"); 
                txtSetName.Text  =DataFunctions.GetFieldValue ("rmSet","SetName","SetID='"+ txtSetID.Text +"'");
                
                string strSQL = string.Empty;
                string strCategoryName = txtCategoryName.Text;
                string strSetName = txtSetName.Text;
                strSQL = "select * from rmCategorySetBOMHdr where CategoryName='" + strCategoryName + "' and SetName='" + strSetName + "'";
                DataTable dtHdr = dbcon.GetDataTable(strSQL);
                foreach (DataRow drRow in dtHdr.Rows)
                {
                    txtQuantity.Text = BaseFunctions.FormatNumeric(drRow["Quantity"], "Ns.8");
                    txtBaseSetSize.Text = BaseFunctions.FormatNumeric(drRow["BaseSetSize"], "Ns.8");
                    txtBaseProductLength.Text = BaseFunctions.FormatNumeric(drRow["BaseProductLength"], "Ns.8");
                    txtBaseProductWidth.Text = BaseFunctions.FormatNumeric(drRow["BaseProductWidth"], "Ns.8");
                    txtBaseMaterialLength.Text = BaseFunctions.FormatNumeric(drRow["BaseMaterialLength"], "Ns.8");
                    txtBaseMaterialWidth.Text = BaseFunctions.FormatNumeric(drRow["BaseMaterialWidth"], "Ns.8");
                    txtBaseMaterialWeight.Text = BaseFunctions.FormatNumeric(drRow["BaseMaterialWeight"], "Ns.8");

                    txtInnerCirclePaperWidth.Text = BaseFunctions.FormatNumeric(drRow["InnerCirclePaperWidth"], "Ns.8");
                    txtInnerWeight.Text = BaseFunctions.FormatNumeric(drRow["InnerWeight"], "Ns.8");
                    txtInnerHeight.Text = BaseFunctions.FormatNumeric(drRow["InnerHeight"], "Ns.8");
                    txtInnerSetSize.Text = BaseFunctions.FormatNumeric(drRow["InnerSetSize"], "Ns.8");
                    txtInnerPages.Text = BaseFunctions.FormatNumeric(drRow["InnerPages"], "Ns.8");
                    txtInnerCutFactor.Text = BaseFunctions.FormatNumeric(drRow["InnerCutFactor"], "Ns.8");
                    txtInnerJoinWidth.Text = BaseFunctions.FormatNumeric(drRow["InnerJoinWidth"], "Ns.8");
                    txtInnerProdLength.Text = BaseFunctions.FormatNumeric(drRow["InnerProdLength"], "Ns.8");
                    txtInnerProdWidth.Text = BaseFunctions.FormatNumeric(drRow["InnerProdWidth"], "Ns.8");
                    txtInnerCutSize.Text = BaseFunctions.FormatNumeric(drRow["InnerCutSize"], "Ns.8");
                    txtInnerJoinLength.Text = BaseFunctions.FormatNumeric(drRow["InnerJoinLength"], "Ns.8");

                    txtFrontCoverHeight.Text = BaseFunctions.FormatNumeric(drRow["FrontCoverHeight"], "Ns.8");
                    txtBackCoverHeight.Text = BaseFunctions.FormatNumeric(drRow["BackCoverHeight"], "Ns.8");
                    txtFrontCoverSize.Text = BaseFunctions.FormatNumeric(drRow["FrontCoverSize"], "Ns.8");
                    txtBackCoverSize.Text = BaseFunctions.FormatNumeric(drRow["BackCoverSize"], "Ns.8");
                    txtBackMateriallFactor.Text = BaseFunctions.FormatNumeric(drRow["BackMateriallFactor"], "Ns.8");
                    txtBackPSFactor.Text = BaseFunctions.FormatNumeric(drRow["BackPSFactor"], "Ns.8");
                    txtPagePrintLength.Text = BaseFunctions.FormatNumeric(drRow["PagePrintLength"], "Ns.8");
                    txtPagePrintWidth.Text = BaseFunctions.FormatNumeric(drRow["PagePrintWidth"], "Ns.8");
                    txtCoverJoinLength.Text = BaseFunctions.FormatNumeric(drRow["CoverJoinLength"], "Ns.8");
                    txtCoverJoinWidth.Text = BaseFunctions.FormatNumeric(drRow["CoverJoinWidth"], "Ns.8");
                    txtCoverMaterialLength.Text = BaseFunctions.FormatNumeric(drRow["CoverMaterialLength"], "Ns.8");
                    txtCoverMaterialWidth.Text = BaseFunctions.FormatNumeric(drRow["CoverMaterialWidth"], "Ns.8");
                    txtBackPSQty1.Text = BaseFunctions.FormatNumeric(drRow["BackPSQty1"], "Ns.8");
                    txtBackPSQty2.Text = BaseFunctions.FormatNumeric(drRow["BackPSQty2"], "Ns.8");
                    txtBackPSQty3.Text = BaseFunctions.FormatNumeric(drRow["BackPSQty3"], "Ns.8");
                    txtBackPSQty4.Text = BaseFunctions.FormatNumeric(drRow["BackPSQty4"], "Ns.8");
                    txtBackCutFactor1.Text = BaseFunctions.FormatNumeric(drRow["BackCutFactor1"], "Ns.8");
                    txtBackCutFactor2.Text = BaseFunctions.FormatNumeric(drRow["BackCutFactor2"], "Ns.8");
                    txtBackPrintInnerQty.Text = BaseFunctions.FormatNumeric(drRow["BackPrintInnerQty"], "Ns.8");
                    txtBackPrintInnerQty2.Text = BaseFunctions.FormatNumeric(drRow["BackPrintInnerQty2"], "Ns.8");
                    txtLaminatingLength.Text = BaseFunctions.FormatNumeric(drRow["LaminatingLength"], "Ns.8");
                    txtLaminatingWidth.Text = BaseFunctions.FormatNumeric(drRow["LaminatingWidth"], "Ns.8");

                    txtHCWeight.Text = BaseFunctions.FormatNumeric(drRow["HCWeight"], "Ns.8");
                    txtHCSetSize.Text = BaseFunctions.FormatNumeric(drRow["HCSetSize"], "Ns.8");
                    txtHCHeight.Text = BaseFunctions.FormatNumeric(drRow["HCHeight"], "Ns.8");
                    txtHCPages.Text = BaseFunctions.FormatNumeric(drRow["HCPages"], "Ns.8");

                    txtStEDPageWeigth.Text = BaseFunctions.FormatNumeric(drRow["StEDPageWeigth"], "Ns.8");
                    txtStEDPageSetSize.Text = BaseFunctions.FormatNumeric(drRow["StEDPageSetSize"], "Ns.8");
                    txtStEDPageHeight.Text = BaseFunctions.FormatNumeric(drRow["StEDPageHeight"], "Ns.8");
                    txtStEDPagePages.Text = BaseFunctions.FormatNumeric(drRow["StEDPagePages"], "Ns.8");
                    txtStEDPageCirclePaperWidth.Text = BaseFunctions.FormatNumeric(drRow["StEDPageCirclePaperWidth"], "Ns.8");  

                    txtPKGContainQty.Text = BaseFunctions.FormatNumeric(drRow["PKGContainQty"], "Ns.8");
                    txtPKGPackageQty.Text = BaseFunctions.FormatNumeric(drRow["PKGPackageQty"], "Ns.8");
                    txtPKGInnerDiameterLength.Text = BaseFunctions.FormatNumeric(drRow["PKGInnerDiameterLength"], "Ns.8");
                    txtPKGInnerDiameterWidth.Text = BaseFunctions.FormatNumeric(drRow["PKGInnerDiameterWidth"], "Ns.8");
                    txtPKGInnerDiameterHeight.Text = BaseFunctions.FormatNumeric(drRow["PKGInnerDiameterHeight"], "Ns.8");
                    txtPKGRows.Text = BaseFunctions.FormatNumeric(drRow["PKGRows"], "Ns.8");
                    txtPKGQty.Text = BaseFunctions.FormatNumeric(drRow["PKGQty"], "Ns.8");

                    txtOthersLockingWireQty.Text = BaseFunctions.FormatNumeric(drRow["OthersLockingWireQty"], "Ns.8");
                    txtOthersLockingWireLength.Text = BaseFunctions.FormatNumeric(drRow["OthersLockingWireLength"], "Ns.8");
                    txtOthersBackWidth.Text = BaseFunctions.FormatNumeric(drRow["OthersBackWidth"], "Ns.8");
                    txtOthersUseIronNailQty.Text = BaseFunctions.FormatNumeric(drRow["OthersUseIronNailQty"], "Ns.8");
                    txtOthersMiddleWidth.Text = BaseFunctions.FormatNumeric(drRow["OthersMiddleWidth"], "Ns.8");
                    txtOthersSheetSize.Text = BaseFunctions.FormatNumeric(drRow["OthersSheetSize"], "Ns.8");
                    txtOthersBurrWidth.Text = BaseFunctions.FormatNumeric(drRow["OthersBurrWidth"], "Ns.8");
                    txtOthersWireLength.Text = BaseFunctions.FormatNumeric(drRow["OthersWireLength"], "Ns.8");
                    txtOthersMiddleSize.Text = BaseFunctions.FormatNumeric(drRow["OthersMiddleSize"], "Ns.8");
                    txtOthersSheetLength.Text = BaseFunctions.FormatNumeric(drRow["OthersSheetLength"], "Ns.8");
                    txtOthersSheetWidth.Text = BaseFunctions.FormatNumeric(drRow["OthersSheetWidth"], "Ns.8");
                    txtOthersOneSheetLength.Text = BaseFunctions.FormatNumeric(drRow["OthersOneSheetLength"], "Ns.8");

                    txtOthersBackQty.Text = BaseFunctions.FormatNumeric(drRow["OthersBackQty"], "Ns.8");
                    txtOthersBackPaperQty.Text = BaseFunctions.FormatNumeric(drRow["OthersBackPaperQty"], "Ns.8");
                    txtOthersCopyPaperPS1.Text = BaseFunctions.FormatNumeric(drRow["OthersCopyPaperPS1"], "Ns.8");
                    txtOthersCopyPaperPS2.Text = BaseFunctions.FormatNumeric(drRow["OthersCopyPaperPS2"], "Ns.8");

                     txtPKGInQty.Text = BaseFunctions.FormatNumeric(drRow["PKGInQty"], "Ns.8");
                     txtPKGInLength.Text = BaseFunctions.FormatNumeric(drRow["PKGInLength"], "Ns.8");
                     txtPKGInWidth.Text = BaseFunctions.FormatNumeric(drRow["PKGInWidth"], "Ns.8");
                     txtPKGInHeight.Text = BaseFunctions.FormatNumeric(drRow["PKGInHeight"], "Ns.8");


                     txtHCJoinLength.Text = BaseFunctions.FormatNumeric(drRow["HCJoinLength"], "Ns.8");
                     txtHCJoinWidth.Text = BaseFunctions.FormatNumeric(drRow["HCJoinWidth"], "Ns.8");
                     txtHCSingleLength.Text = BaseFunctions.FormatNumeric(drRow["HCSingleLength"], "Ns.8");
                     txtHCSingleWidth.Text = BaseFunctions.FormatNumeric(drRow["HCSingleWidth"], "Ns.8");

                     txtHCSplitWeight.Text = BaseFunctions.FormatNumeric(drRow["HCSplitWeight"], "Ns.8");
                     txtHCSplitSet.Text = BaseFunctions.FormatNumeric(drRow["HCSplitSet"], "Ns.8");
                     txtHCSplitHeight.Text = BaseFunctions.FormatNumeric(drRow["HCSplitHeight"], "Ns.8");
                     txtHCSplitQty.Text = BaseFunctions.FormatNumeric(drRow["HCSplitQty"], "Ns.8");



                     txtStEDPageJoinLength.Text = BaseFunctions.FormatNumeric(drRow["StEDPageJoinLength"], "Ns.8");
                     txtStEDPageJoinWidth.Text = BaseFunctions.FormatNumeric(drRow["StEDPageJoinWidth"], "Ns.8");
                     txtStEDPageSplitSize.Text = BaseFunctions.FormatNumeric(drRow["StEDPageSplitSize"], "Ns.8");


                     txtOthersLineQty.Text = BaseFunctions.FormatNumeric(drRow["OthersLineQty"], "Ns.8");

                }
            }
            dbcon.CloseCon();
        }
        protected override void Field_KeyPress(object sender, KeyPressEventArgs e)
        {
            base.Field_KeyPress(sender, e);
        }

        private void tbcMain_Selecting(object sender, TabControlCancelEventArgs e)
        {
            if ((this.formStatus == FormStatus.Query || this.formStatus == FormStatus.Delete) &&
                this.dgList.Rows.Count == 0 && e.TabPageIndex > 0)
            {
                e.Cancel = true;
                return;
            }

            if (e.TabPageIndex == 0)
            {
                if (this.formStatus == FormStatus.AddNew || this.formStatus == FormStatus.Update)
                {
                    if (this.ConfirmChange())
                    {
                        if (MessageBox.Show(SysVar.SaveChangeTitle, SysVar.WarnTitle, MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            if (!this.UpdateData())
                            {
                                if (m_bTabPageNeedCancel)
                                    e.Cancel = true;

                                m_bTabPageNeedCancel = true;
                                return;
                            }
                        }
                    }
                    this.formStatus = FormStatus.Delete;
                }
                else if (this.formStatus == FormStatus.Query)
                {
                    this.formStatus = FormStatus.Delete;
                }
                this.dgList.RefreshData();
                this.dgList.Focus();
            }
            else if (e.TabPage == tbpRMInfo && this.formStatus == FormStatus.AddNew )
            {
                this.UpdateData();
            }
            else if (e.TabPageIndex != 0 && this.formStatus != FormStatus.AddNew)
            {
                if (this.RecID == -1)
                    this.RecID = Convert.ToInt64(dgList.RecID);
            }
        }
        private void tbcMain_DrawItem(object sender, DrawItemEventArgs e)
        {
            BaseFunctions.TabControl_DrawItem(sender, e, true);
        }
        private void FileMenu_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem mnuItem = sender as ToolStripMenuItem;
            if (mnuItem != null)
            {
                switch (mnuItem.Name)
                {
                    case "newToolStripMenuItem":
                        btnNew_Click(sender, e);
                        break;
                    case "cancelToolStripMenuItem":
                        btnCancel_Click(sender, e);
                        break;
                    case "deleteToolStripMenuItem":
                        btnDelete_Click(sender, e);
                        break;
                    case "saveToolStripMenuItem":
                        btnUpdate_Click(sender, e);
                        break;
                    case "exitToolStripMenuItem":
                        btnExit_Click(sender, e);
                        break;
                }
            }
        }

        #endregion Form Events***

        #region ***Form Functions

        public override bool UpdateData()
        {
            bool bUpdateResult = true;
            if (base.ConfirmChange())
            {
                if (txtProductFItemID.Text == "" || txtProductFItemID.Text  =="0")
                {
                    txtProductFItemID.Text = CvtFunc.ToString (90000000 + CvtFunc.ToDouble( DataFunctions.GetFieldValue("rmProduct", "max(RecID)", "1=1"))+1);
                }
                bUpdateResult = base.UpdateData();
                RMProductBOMHdrDAL rmDAL = new RMProductBOMHdrDAL();
                DBAccess dbcon = new DBAccess(false, false);
                try
                {
                    dbcon.BeginTran();                   
                    string strSQL = "select * from rmProductBOMDetail where productFItemID='" + txtProductFItemID.Text + "'";
                    DataTable dtBOM = dbcon.GetDataTable(strSQL);
                    foreach (DataRow drBOM in dtBOM.Rows)
                    {
                        rmDAL.UpdateBOMDetail(dbcon, drBOM);
                    }

                    strSQL = "select * from rmProductBOMLabourCost where productFItemID='" + txtProductFItemID.Text + "'";
                    DataTable dtLabourCost = dbcon.GetDataTable(strSQL);
                    foreach (DataRow drLabourCost in dtLabourCost.Rows)
                    {
                        rmDAL.UpdateLabourCostDetail(dbcon, drLabourCost); 
                    }
                    dbcon.CommitTran(); 
                }
                catch (Exception ex)
                {
                    dbcon.RollbackTran();
                    MessageBox.Show(ex.ToString());
                    return false;
                }
            }
            return bUpdateResult;
        }
        public override bool ConfirmChange()
        {
            return (base.ConfirmChange());
        }
        protected override bool FieldCheck(string strFieldValue, string strFieldName, ref string strErrPrompt)
        {

            strErrPrompt = string.Empty;
            DBAccess dBAccess = new DBAccess(false, false);
            DataRow dr = base.GetCurrentMtnDataRow();


            RM.BusinessRule.RMProductBRL mBRL = new WP.RM.BusinessRule.RMProductBRL();
            bool bCheckResult = mBRL.FieldCheck(dBAccess, dr, strFieldName, ref strErrPrompt, this.formStatus);
            dBAccess.CloseCon();
            return bCheckResult;

        }
        protected override string GetLookUpSQL(string strTableName, string strLookUpControlName)
        {
            string strSql = string.Empty;
            if (strLookUpControlName == ubtSetName.Name)
            {
                if (txtCategoryID.Text != "")
                {
                    return strSql = "SELECT RecId, SetID, SetName,length,Width,Height,GramWeight,Remark FROM rmSet where SetID like '" + txtCategoryID.Text + "%' order by SetID ";
                }
            }
            return base.GetLookUpSQL(strTableName, strLookUpControlName);
        }
        public override void SetButtonStatus()
        {
            switch (this.formStatus)
            {
                case FormStatus.AddNew:
                    BaseFunctions.ControlEnabled(this.toolStrip, false);
                    this.btnUpdate.Enabled = true;
                    this.btnDelete.Enabled = false;
                    this.btnNew.Enabled = false ;
                    this.btnCancel.Enabled = true;
                    break;
                case FormStatus.Delete:
                    BaseFunctions.ControlEnabled(this.toolStrip, false);
                    this.btnUpdate.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnNew.Enabled = (true  && this.CurrentMenuInfo.strPermit == "Y");
                    this.btnCancel.Enabled = false;
                    break;
                case FormStatus.Query:
                    BaseFunctions.ControlEnabled(this.toolStrip, false);
                    this.btnUpdate.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnNew.Enabled = (true && this.CurrentMenuInfo.strPermit == "Y");
                    this.btnCancel.Enabled = false;
                    break;
                case FormStatus.Update:
                    BaseFunctions.ControlEnabled(this.toolStrip, false);
                    this.btnUpdate.Enabled = true;
                    this.btnDelete.Enabled = true;
                    this.btnNew.Enabled = false ;
                    this.btnCancel.Enabled = true;
                    break;
                case FormStatus.Loading:
                    break;

            }

            this.btnToExcel.Visible = (this.formStatus == FormStatus.Delete || this.formStatus == FormStatus.Query) && SysVar.AbleToExcel;
            this.btnToExcel.Enabled = this.formStatus == FormStatus.Delete || this.formStatus == FormStatus.Query;

            newToolStripMenuItem.Enabled = btnNew.Enabled;
            cancelToolStripMenuItem.Enabled = btnCancel.Enabled;
            deleteToolStripMenuItem.Enabled = btnDelete.Enabled;
            saveToolStripMenuItem.Enabled = btnUpdate.Enabled;

            this.btnTop.Enabled = true;
            this.btnBottom.Enabled = true;
            this.btnPrev.Enabled = true;
            this.btnNext.Enabled = true;
            this.btnHelp.Enabled = true;
            this.btnExit.Enabled = true;
            this.btnLookStandardBOM.Enabled = true;
            this.btnLookStandardBOM.Visible = false;
        }

        public override void SetControlEditStatus()
        {
            base.SetControlEditStatus();
            txtSetName.ReadOnly = true;
            txtCategoryName.ReadOnly = true;
        }
        public override void SetOtherStatus()
        {
            mblnSettingStatus = true;

            switch (this.formStatus)
            {
                case FormStatus.AddNew:
                    this.txtProductID.Focus();
                    break;
                case FormStatus.Delete:
                    break;
                case FormStatus.Query:
                    break;
                case FormStatus.Update:
                    break;
                case FormStatus.Loading:
                    break;
            }

            mblnSettingStatus = false;
        }
        public override void RefreshOverView()
        {
            base.RefreshOverView();
            if (this.RecID != -1)
                this.dgList.RecID = this.RecID;
        }

        private void FillLookUpInfo()
        {
            SetFieldLookUpInfo("CategoryID", "rmCategory", "CategoryID", "ubtCategoryName");
            SetFieldLookUpInfo("CategoryName", "rmCategory", "CategoryName", "ubtCategoryName");
            SetFieldLookUpInfo("SetID", "rmSet", "SetID", "ubtSetName");
            SetFieldLookUpInfo("SetName", "rmSet", "SetName", "ubtSetName");
        }

        #endregion Form Functions***

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            string strMenuID = DataFunctions.GetFieldValue ("mMenu","MenuID","Net_ProgName='frmRMProductBOMMtn'");

            bool blnPermission = (Permission.GetMenuSecurity(strMenuID)=="Y" ? true : false);
            if (!blnPermission)
            {
                MsgBoxEx.Show("您可能没有权限，请联系系统管理员", SysVar.WarnTitle);
            }
            else
            {
                string strProductName = txtProductName.Text;
                if (this.formStatus == FormStatus.Query || this.formStatus == FormStatus.Delete )
                {
                    if (this.dgList.CurrentRow == null) return;
                    strProductName = CvtFunc.ToString(this.dgList.CurrentRow.Cells["ProductName"].Value);                    
                }
                if (strProductName == "") return;
                string strRecID = DataFunctions.GetFieldValue("rmProduct", "RecID", "ProductName='" + strProductName + "'");
                MenuInfo menuInfo = DataFunctions.GetMenuInfo(DataFunctions.GetFieldValue( "mMenu", "MenuId", "net_ProgName='frmRMProductBOMMtn' and net_Parameter=0"));
                ReflectMethod.ShowForm(menuInfo, FormStatus.Update, CvtFunc.ToLong(strRecID));
            }
        }

        
        private void tvTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Nodes.Count == 0) //最后一层
            {
                string strMaterialID = "";
                strMaterialID = CvtFunc.ToString(e.Node.Text.ToString()).Split('-')[0];
                //this.dgList.FindRecord(strMaterialID, "MaterialTypeID");
                string strCond = " ProductID like '" + strMaterialID + "%'";
                ShowOverViewGrid(strCond);
            }
            else  //最上层
            {
                string strMaterialID = "";
                strMaterialID = CvtFunc.ToString(e.Node.Text.ToString()).Split('-')[0];
                //this.dgList.FindRecord(strMaterialID, "MaterialTypeID");
                string strCond = " ProductID like '" + strMaterialID + "%'";
                ShowOverViewGrid(strCond);
            }
        }

        private void ShowOverViewGrid(string strWhereCondition)
        {
            string strSql = "";
            if (dgList.Sql.Trim() == "")
                strSql = WP.Commons.BaseFunctions.GetLookUpSQL(this.TableName, this.CurrentMenuInfo.iParameter);
            else
                strSql = dgList.Sql;

            if (strWhereCondition != "")
            {
                string strOrderBy = BaseFunctions.GetSqlParts(strSql, SqlOption.SqlPartOrderBy);
                strSql = BaseFunctions.GetSqlParts(strSql, SqlOption.SqlPartMain) + " where " + strWhereCondition;
                strSql += strOrderBy;
            }
            //string strWhere = BaseFunctions.GetSqlParts(strSql, SqlOption.SqlPartWhere);
            //mstrWhereCondition = strWhere;
            this.dgList.Sql = strSql;
        }

    }
}
